package com.ruoyi.gateway.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gateway.mapper.MessagePropertyMapMapper;
import com.ruoyi.gateway.domain.MessagePropertyMap;
import com.ruoyi.gateway.service.IMessagePropertyMapService;

/**
 * 消息属性映射Service业务层处理
 *
 * @author 白向阳
 * @date 2025-09-28
 */
@Service
public class MessagePropertyMapServiceImpl implements IMessagePropertyMapService
{
    @Autowired
    private MessagePropertyMapMapper propertyMappper;

    // 缓存属性映射关系
    private final Map<String, Map<String, String>> propertyCache = new HashMap<>();

    /**
     * 获取平台属性名
     */
    @Override
    public String getPlatformProperty(String direction, String deviceProperty) {
        loadPropertyCacheIfNeeded();

        String cacheKey = direction + ":deviceToPlatform";
        Map<String, String> mapping = propertyCache.get(cacheKey);
        return mapping != null ? mapping.get(deviceProperty) : null;
    }

    /**
     * 获取设备属性名
     */
    @Override
    public String getDeviceProperty(String direction, String platformProperty) {
        loadPropertyCacheIfNeeded();

        String cacheKey = direction + ":platformToDevice";
        Map<String, String> mapping = propertyCache.get(cacheKey);
        return mapping != null ? mapping.get(platformProperty) : null;
    }

    /**
     * 加载属性映射缓存
     */
    private synchronized void loadPropertyCacheIfNeeded() {
        if (propertyCache.isEmpty()) {
            List<MessagePropertyMap> allMappings = propertyMappper.selectAll();

            for (MessagePropertyMap mapping : allMappings) {
                String direction = mapping.getDirection();
                String devicePath = mapping.getDevicePropertyPath();
                String platformPath = mapping.getPlatformPropertyPath();

                // 设备到平台映射
                String deviceToPlatformKey = direction + ":deviceToPlatform";
                propertyCache.computeIfAbsent(deviceToPlatformKey, k -> new HashMap<>())
                        .put(devicePath, platformPath);

                // 平台到设备映射
                String platformToDeviceKey = direction + ":platformToDevice";
                propertyCache.computeIfAbsent(platformToDeviceKey, k -> new HashMap<>())
                        .put(platformPath, devicePath);
            }
        }
    }

    /**
     * 刷新属性映射缓存
     */
    @Override
    public void refreshPropertyCache() {
        propertyCache.clear();
        loadPropertyCacheIfNeeded();
    }

    /**
     * 根据设备属性路径获取完整的平台属性映射
     */
    @Override
    public MessagePropertyMap getPropertyMappingByDevicePath(String direction, String devicePropertyPath) {
        return propertyMappper.selectByDevicePath(direction, devicePropertyPath);
    }

    /**
     * 根据平台属性路径获取完整的设备属性映射
     */
    @Override
    public MessagePropertyMap getPropertyMappingByPlatformPath(String direction, String platformPropertyPath) {
        return propertyMappper.selectByPlatformPath(direction, platformPropertyPath);
    }

    /**
     * 查询消息属性映射
     *
     * @param id 消息属性映射主键
     * @return 消息属性映射
     */
    @Override
    public MessagePropertyMap selectMessagePropertyMapById(Long id)
    {
        return propertyMappper.selectMessagePropertyMapById(id);
    }

    /**
     * 查询消息属性映射列表
     *
     * @param messagePropertyMap 消息属性映射
     * @return 消息属性映射
     */
    @Override
    public List<MessagePropertyMap> selectMessagePropertyMapList(MessagePropertyMap messagePropertyMap)
    {
        return propertyMappper.selectMessagePropertyMapList(messagePropertyMap);
    }

    /**
     * 新增消息属性映射
     *
     * @param messagePropertyMap 消息属性映射
     * @return 结果
     */
    @Override
    public int insertMessagePropertyMap(MessagePropertyMap messagePropertyMap)
    {
        messagePropertyMap.setCreateTime(DateUtils.getNowDate());
        return propertyMappper.insertMessagePropertyMap(messagePropertyMap);
    }

    /**
     * 修改消息属性映射
     *
     * @param messagePropertyMap 消息属性映射
     * @return 结果
     */
    @Override
    public int updateMessagePropertyMap(MessagePropertyMap messagePropertyMap)
    {
        messagePropertyMap.setUpdateTime(DateUtils.getNowDate());
        return propertyMappper.updateMessagePropertyMap(messagePropertyMap);
    }

    /**
     * 批量删除消息属性映射
     *
     * @param ids 需要删除的消息属性映射主键
     * @return 结果
     */
    @Override
    public int deleteMessagePropertyMapByIds(Long[] ids)
    {
        return propertyMappper.deleteMessagePropertyMapByIds(ids);
    }

    /**
     * 删除消息属性映射信息
     *
     * @param id 消息属性映射主键
     * @return 结果
     */
    @Override
    public int deleteMessagePropertyMapById(Long id)
    {
        return propertyMappper.deleteMessagePropertyMapById(id);
    }
}
